#include <tr_int.h>
#include <util.h>
#include <msp430/msp430_usart.h>
#include <tiros/tiros.h>




// Setup clock speeds.
#define LFXT1CLK 32.768e3
#define ACLK     LFXT1CLK
#define SMCLK    800000


// Configure USART0
// Maximum USART baud rate is clock/3.
#define BAUD_RATE 	       9600
//#define USART0_BRCLK      SMCLK
//#define USART0_BRSEL      (SSEL_SMCLK)
#define USART0_BRCLK      ACLK
#define USART0_BRSEL      (SSEL_ACLK)


#define ISR_PIN  BIT0



void platform_init(void)
{
	
	WDTCTL = WDTPW|WDTHOLD;               //Init watchdog timer

	while ((IFG1 & OFIFG) != 0) {
		// Wait for LF Osc to startup
		IFG1 &= ~OFIFG;
		delayus(100);
	} 


	/* Setup isr */
	P1IFG &= ~ISR_PIN;
	P1IE  |= ISR_PIN;

	// To use serial port on MSP430
	usart0Init(USART_BAUD_DIV(BAUD_RATE, USART0_BRCLK),
		   USART_BAUD_MOD(BAUD_RATE, USART0_BRCLK), 
		   USART_8N1, USART0_BRSEL); 

}

void isr_action(void);

void platform_call_ISR(void)
{
	/* Set an interrupt */
	P1IFG |= ISR_PIN;
}




/* Interrupt handler */
#if defined(__GNUC__)
interrupt (PORT1_VECTOR) port1_isr_handler(void) __attribute__ ( (naked));
interrupt (PORT1_VECTOR) port1_isr_handler(void)
#elif defined (__ICC430__)
#  pragma vector=PORT1_VECTOR
__raw __interrupt void port1_isr_handler(void)
#else
#  error "Unknown msp430 compiler"
#endif
{
	HALINT_ISR_ENTRY();
	OS_ISR_BEGIN();
	P1IFG &= ~ISR_PIN;
	
	isr_action();
	OS_ISR_END();
	HALINT_ISR_EXIT();
}




